;;; muse-wiki.el --- wiki features for Muse

;; Copyright (C) 2005, 2006 Free Software Foundation, Inc.

;; Author: Yann Hodique <Yann.Hodique@lifl.fr>
;; Keywords:

;; This file is part of Emacs Muse.  It is not part of GNU Emacs.

;; Emacs Muse is free software; you can redistribute it and/or modify
;; it under the terms of the GNU General Public License as published
;; by the Free Software Foundation; either version 2, or (at your
;; option) any later version.

;; Emacs Muse is distributed in the hope that it will be useful, but
;; WITHOUT ANY WARRANTY; without even the implied warranty of
;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
;; General Public License for more details.

;; You should have received a copy of the GNU General Public License
;; along with Emacs Muse; see the file COPYING.  If not, write to the
;; Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
;; Boston, MA 02110-1301, USA.

;;; Commentary:

;;; Contributors:

;;; Code:

(require 'muse-regexps)
(require 'muse-mode)

(eval-when-compile
  (require 'muse-colors))

(defgroup muse-wiki nil
  "Options controlling the behavior of Emacs Muse Wiki features."
  :group 'muse-mode)

(defun muse-wiki-update-wikiword-regexp (sym val)
  "Update everything related to `muse-wiki-wikiword-regexp'."
  (set sym val)
  (when (featurep 'muse-colors)
    (muse-configure-highlighting 'muse-colors-markup muse-colors-markup)))

(defcustom muse-wiki-hide-nop-tag t
  "If non-nil, hide <nop> tags when coloring a Muse buffer."
  :type 'boolean
  :group 'muse-wiki)

(defcustom muse-wiki-wikiword-regexp
  (concat "\\<\\(\\(?:[" muse-regexp-upper
          "]+[" muse-regexp-lower "]+\\)\\(?:["
          muse-regexp-upper "]+[" muse-regexp-lower "]+\\)+\\)")
  "Regexp used to match WikiWords."
  :type 'regexp
  :group 'muse-wiki
  :set 'muse-wiki-update-wikiword-regexp)

(defcustom muse-wiki-use-wikiword t
  "Whether to use color and publish bare WikiNames."
  :type 'boolean
  :group 'muse-wiki)

(defcustom muse-wiki-allow-nonexistent-wikiword nil
  "Whether to color bare WikiNames that don't have an existing file."
  :type 'boolean
  :group 'muse-wiki)

(defcustom muse-wiki-ignore-bare-project-names nil
  "Determine whether project names without a page specifer are links.
If non-nil, project names without a page specifier will not be
considered links.
When nil, project names without a specifier are highlighted and
they link to the default page of the project that they name."
  :type 'boolean
  :group 'muse-wiki)

(defvar muse-wiki-interwiki-regexp ""
  "Regexp that matches all interwiki links.
This is automatically generated by setting `muse-wiki-interwiki-alist'.
It can also be set by calling `muse-wiki-update-interwiki-regexp'.")

(defcustom muse-wiki-interwiki-delimiter "#\\|::"
  "Delimiter regexp used for InterWiki links.
If you use groups, use only shy groups."
  :type 'regexp
  :group 'muse-wiki)

(defcustom muse-wiki-interwiki-replacement ": "
  "Regexp used for replacing `muse-wiki-interwiki-delimiter' in
InterWiki link descriptions.

If you want this replacement to happen, you must add
`muse-wiki-publish-pretty-interwiki' to
`muse-publish-desc-transforms'."
  :type 'regexp
  :group 'muse-wiki)

(defun muse-wiki-update-interwiki-regexp (value)
  "Update the value of `muse-wiki-interwiki-regexp' based on VALUE
and `muse-project-alist'."
  (setq muse-wiki-interwiki-regexp
        (concat "\\<\\(" (mapconcat 'car muse-project-alist "\\|")
                (when value (concat "\\|" (mapconcat 'car value "\\|")))
                "\\)\\(?:\\(?:" muse-wiki-interwiki-delimiter
                "\\)\\(\\sw+\\)\\)?\\>"))
  (when (featurep 'muse-colors)
    (muse-configure-highlighting 'muse-colors-markup muse-colors-markup)))

(defcustom muse-wiki-interwiki-alist
  '(("EmacsWiki" . "http://www.emacswiki.org/cgi-bin/wiki/"))
  "A table of WikiNames that refer to external entities.
The format of this table is an alist, or series of cons cells.
Each cons cell must be of the form:

  (WIKINAME . STRING-OR-FUNCTION)

The second part of the cons cell may either be a STRING, which in most
cases should be a URL, or a FUNCTION.  If a function, it will be
called with one argument: the tag applied to the Interwiki name, or
nil if no tag was used.  If the cdr was a STRING and a tag is used,
the tag is simply appended.

Here are some examples:

  (\"JohnWiki\" . \"http://alice.dynodns.net/wiki?\")

Referring to [[JohnWiki::EmacsModules]] then really means:

  http://alice.dynodns.net/wiki?EmacsModules

If a function is used for the replacement text, you can get creative
depending on what the tag is.  Tags may contain any alphabetic
character, any number, % or _.  If you need other special characters,
use % to specify the hex code, as in %2E.  All browsers should support
this."
  :type '(repeat (cons (string :tag "WikiName")
                       (choice (string :tag "URL") function)))
  :set (function
        (lambda (sym value)
          (muse-wiki-update-interwiki-regexp value)
          (set sym value)))
  :group 'muse-wiki)

(defun muse-wiki-resolve-project-page (&optional project page)
  "Return the published path from the current page to PAGE of PROJECT.
If PAGE is not specified, use the value of :default in PROJECT.
If PROJECT is not specified, default to first project of
`muse-projects-alist'.

Note that PAGE can have several output directories.  If this is
the case, we will use the first one that matches our current
style and ignore the others."
  (setq project (or project (caar muse-project-alist))
        page (or page (muse-get-keyword :default
                                        (cadr (muse-project project)))))
  (let* ((page-path (muse-project-page-file page project))
         (remote-style (when page-path (car (muse-project-applicable-styles
                                             page-path project))))
         (local-style (car (muse-project-applicable-styles
                            (muse-current-file)
                            (cddr (muse-project-of-file))))))
    (cond ((and remote-style local-style muse-publishing-p)
           (let ((prefix (muse-style-element :base-url remote-style)))
             (muse-publish-link-file
              (if prefix
                  (concat prefix page)
                (file-relative-name (expand-file-name
                                     page
                                     (muse-style-element :path remote-style))
                                    (expand-file-name
                                     (muse-style-element :path local-style))))
              nil remote-style)))
          ((not muse-publishing-p)
           (if page-path
               page-path
             (when muse-wiki-allow-nonexistent-wikiword
               ;; make a path to a nonexistent file in project
               (setq page-path (expand-file-name
                                page (car (cadr (muse-project project)))))
               (if (and muse-file-extension
                        (not (string= muse-file-extension "")))
                   (concat page-path "." muse-file-extension)
                 page-path)))))))

(defun muse-wiki-handle-interwiki (&optional string)
  "If STRING or point has an interwiki link, resolve it and
return the first match.
Match 1 is set to the link.
Match 2 is set to the description."
  (when (if string (string-match muse-wiki-interwiki-regexp string)
          (looking-at muse-wiki-interwiki-regexp))
    (let* ((project (match-string 1 string))
           (subst (cdr (assoc project muse-wiki-interwiki-alist)))
           (word (if string
                     (and (match-beginning 2)
                          (substring string (match-beginning 2)))
                   (match-string 2 string))))
      (if subst
          (if (functionp subst)
              (funcall subst word)
            (concat subst word))
        (and (assoc project muse-project-alist)
             (or word (not muse-wiki-ignore-bare-project-names))
             (muse-wiki-resolve-project-page project word))))))

(defun muse-wiki-handle-wikiword (&optional string)
  "If STRING or point has a WikiWord, return it.
Match 1 is set to the WikiWord."
  (when (and muse-wiki-use-wikiword
             (if string
                 (string-match muse-wiki-wikiword-regexp string)
               (looking-at muse-wiki-wikiword-regexp))
             (or muse-wiki-allow-nonexistent-wikiword
                 (and (muse-project-of-file)
                      (muse-project-page-file
                       (match-string 1 string) muse-current-project t))
                 (file-exists-p (match-string 1 string))))
    (match-string 1 string)))

;; Prettifications

(defcustom muse-wiki-publish-small-title-words
  '("the" "and" "at" "on" "of" "for" "in" "an" "a")
  "Strings that should be downcased in a page title.
This is used by `muse-wiki-publish-pretty-title', which must be
called manually."
  :type '(repeat string)
  :group 'muse-wiki)

(defun muse-wiki-publish-pretty-title (&optional title explicit)
  "Return a pretty version of the given TITLE.
If EXPLICIT is non-nil, TITLE will be returned unmodified."
  (unless title (setq title (muse-publishing-directive "title")))
  (if (or explicit
          (save-match-data (string-match muse-url-regexp title)))
      title
    (save-match-data
      (let ((case-fold-search nil))
        (while (string-match (concat "\\([" muse-regexp-lower
                                     "]\\)\\([" muse-regexp-upper
                                     "0-9]\\)")
                             title)
          (setq title (replace-match "\\1 \\2" t nil title)))
        (let* ((words (split-string title))
               (w (cdr words)))
          (while w
            (if (member (downcase (car w))
                        muse-wiki-publish-small-title-words)
                (setcar w (downcase (car w))))
            (setq w (cdr w)))
          (mapconcat 'identity words " "))))))

(defun muse-wiki-publish-pretty-interwiki (desc &optional explicit)
  "Replace instances of `muse-wiki-interwiki-delimiter' with
`muse-wiki-interwiki-replacement'."
  (if (or explicit
          (save-match-data (string-match muse-url-regexp desc)))
      desc
    (muse-replace-regexp-in-string muse-wiki-interwiki-delimiter
                                   muse-wiki-interwiki-replacement
                                   desc)))

;; Coloring setup

(eval-after-load "muse-colors"
  '(progn
     (defun muse-wiki-colors-nop-tag (beg end)
       (add-text-properties beg (+ beg 5)
                            '(invisible muse intangible t)))
     (defun muse-colors-wikiword-separate ()
       (add-text-properties (match-beginning 0) (match-end 0)
                            '(invisible muse intangible t)))

     (add-to-list 'muse-colors-tags
                  '("nop" nil nil muse-wiki-colors-nop-tag)
                  t)

     (add-to-list 'muse-colors-markup
                  '(muse-wiki-interwiki-regexp t muse-colors-implicit-link)
                  t)
     (add-to-list 'muse-colors-markup
                  '(muse-wiki-wikiword-regexp t muse-colors-implicit-link)
                  t)
     (add-to-list 'muse-colors-markup
                  '("''''" ?\' muse-colors-wikiword-separate)
                  nil)

     (muse-configure-highlighting 'muse-colors-markup muse-colors-markup)))

;; Publishing setup

(eval-after-load "muse-publish"
  '(progn
     (add-to-list 'muse-publish-markup-regexps
                  '(3100 muse-wiki-interwiki-regexp 0 link)
                  t)
     (add-to-list 'muse-publish-markup-regexps
                  '(3200 muse-wiki-wikiword-regexp 0 link)
                  t)
     (add-to-list 'muse-publish-markup-regexps
                  '(3300 "''''" 0 "")
                  t)

     (custom-add-option 'muse-publish-desc-transforms
                        'muse-wiki-publish-pretty-interwiki)
     (custom-add-option 'muse-publish-desc-transforms
                        'muse-wiki-publish-pretty-title)))

;; Insinuate link handling

(custom-add-option 'muse-implicit-link-functions
                   'muse-wiki-handle-interwiki)
(custom-add-option 'muse-implicit-link-functions
                   'muse-wiki-handle-wikiword)

(custom-add-option 'muse-explicit-link-functions
                   'muse-wiki-handle-interwiki)

(add-to-list 'muse-implicit-link-functions
             'muse-wiki-handle-interwiki t)
(add-to-list 'muse-implicit-link-functions
             'muse-wiki-handle-wikiword t)

(add-to-list 'muse-explicit-link-functions
             'muse-wiki-handle-interwiki t)

;; Update several things when Muse mode is entered
(defun muse-wiki-update-custom-values ()
  "Update some important muse-wiki values that may have been altered manually."
  (muse-wiki-update-interwiki-regexp muse-wiki-interwiki-alist))

(custom-add-option 'muse-mode-hook
                   'muse-wiki-update-custom-values)

(add-hook 'muse-mode-hook
          'muse-wiki-update-custom-values)

(provide 'muse-wiki)
;;; muse-wiki.el ends here
